import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String enteredCaptcha = request.getParameter("captcha");

        HttpSession session = request.getSession();
        String storedCaptcha = (String) session.getAttribute("captcha");

        if (enteredCaptcha != null && enteredCaptcha.equals(storedCaptcha)) {
            // 验证码正确，继续处理登录
            if ("admin".equals(username) && "password".equals(password)) {
                // 登录成功
                response.getWriter().write("Login successful");
            } else {
                // 登录失败
                response.getWriter().write("Login failed");
            }
        } else {
            // 验证码错误
            response.getWriter().write("Invalid captcha");
        }
    }
}
